﻿using MediatR;
using Microsoft.IdentityModel.Tokens;
using WMS.API.Application.Command.StocktakingManagement.PendingOrder;
using WMS.Domain.Entities.StocktakingManagement;
using WMS.ErrorCode;
using WMS.Interfactructure.Interfaces;

namespace WMS.API.Application.CommandHandlers.StocktakingManagement
{
    /// <summary>
    /// 查询待盘点订单命令-xzc
    /// </summary>
    public class QueryPendingOrdersCommandHandlers : IRequestHandler<QueryPendingOrdersCommand, APIResult<List<PendingOrders>>>
    {
        private readonly IBaseRepository<PendingOrders> _pendingOrdersRepository;

        public QueryPendingOrdersCommandHandlers(IBaseRepository<PendingOrders> pendingOrdersRepository)
        {
            _pendingOrdersRepository = pendingOrdersRepository;
        }

        public Task<APIResult<List<PendingOrders>>> Handle(QueryPendingOrdersCommand request, CancellationToken cancellationToken)
        {
            APIResult<List<PendingOrders>> result = new APIResult<List<PendingOrders>>("查询带盘点订单成功");
            result.Code = ResultCode.Success;

            var list = _pendingOrdersRepository.GetAll();

            if (!string.IsNullOrEmpty(request.Warehouse))
            {
                list = list.Where(m => m.Warehouse == request.Warehouse);
            }
            if (!string.IsNullOrEmpty(request.Team))
            {
                list = list.Where(m => m.Team == request.Team);
            }
            if (!string.IsNullOrEmpty(request.InventoryCode))
            {
                list = list.Where(m => m.InventoryCode == request.InventoryCode);
            }
            result.Data = list.ToList();

            return Task.FromResult(result);
        }
    }
}
